<?php

namespace C\lib {

    class Page
    {
        public static $firstRow; // 起始行数
        public static $listRows; // 列表每页显示行数
        public static $parameter; // 分页跳转时要带的参数
        public static $totalRows; // 总行数
        public static $totalPages; // 分页总页面数
        public static $rollPage = 5;// 分页栏每页显示的页数
        public static $lastSuffix = true; // 最后一页是否显示总页数

        private static $nowPage = 1;
        private static $p = 'p'; //分页参数名
        private static $url = ''; //当前链接URL

        // 分页显示定制
        private static $config = [
            'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
            'prev' => '<<',
            'next' => '>>',
            'first' => '1...',
            'last' => '...%TOTAL_PAGE%',
            'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%'
        ];

        /**
         * @param array $totalRows 总的记录数
         * @param int   $listRows  每页显示记录数
         * @param int   $nowp      当前页
         */
        public static function setParam($totalRows, $listRows = 5, $nowp = 1 , $rollPage=5)
        {
            /* 基础设置 */
            self::$totalRows = $totalRows; //设置总记录数
            self::$listRows = $listRows;  //设置每页显示行数
            self::$nowPage = intval($nowp);
            self::$nowPage = self::$nowPage > 0 ? self::$nowPage : 1;
            self::$firstRow = self::$listRows * (self::$nowPage - 1);
            self::$rollPage = intval($rollPage);
        }

        /**
         * 定制分页链接设置
         * @param string $name 设置名称
         * @param string $value 设置值
         */
        public static function setConfig($name , $value)
        {
            if(isset(self::$config[$name])) {
                self::$config[$name] = $value;
            }
        }

        /**
         * 生成链接URL
         * @param  integer $page 页码
         * @return string
         */
        private static function url($page)
        {
//            return str_replace(urlencode('[PAGE]'), $page, self::$url);
        }

        /**
         * 组装分页链接
         * @return string
         */
        public static function show()
        {
            if(0 == self::$totalRows) return '';

            /* 计算分页信息 */

            self::$totalPages = ceil(self::$totalRows / self::$listRows); //总页数

            if(!empty(self::$totalPages) && self::$nowPage > self::$totalPages) {
                self::$nowPage = self::$totalPages;
            }

            /* 计算分页零时变量 */
            $now_cool_page = self::$rollPage / 2;
            $now_cool_page_ceil = ceil($now_cool_page);

            $up_row = self::$nowPage - 1;
            $up_page =  '<a class="prev" href="javascript:;" page="'.($up_row > 0 ? $up_row: 1).'">' . self::$config['prev'] . '</a>';

            $down_row = self::$nowPage + 1;
            $down_page = '<a class="next" href="javascript:;"  page="'.($down_row <= self::$totalPages ?$down_row:self::$totalPages).'">' . self::$config['next'] . '</a>';

            $the_first = '<a class="first" href="javascript:;"   page="1">' . self::$config['first'] . '</a>';

            $the_end = '<a class="end" href="javascript:;"  page="'.self::$totalPages.'">' . self::$config['last'] . '</a>';

            //数字连接
            $link_page = "";
            for($i = 1; $i <= self::$rollPage; $i++) {
                if((self::$nowPage - $now_cool_page) <= 0) {
                    $page = $i;
                } elseif((self::$nowPage + $now_cool_page - 1) >= self::$totalPages) {
                    $page = self::$totalPages - self::$rollPage + $i;
                } else {
                    $page = self::$nowPage - $now_cool_page_ceil + $i;
                }
                if($page > 0 && $page != self::$nowPage) {

                    if($page <= self::$totalPages) {
                        $link_page .= '<a class="num" href="javascript:;"  page="'.$page.'">' . $page . '</a>';
                    } else {
                        break;
                    }
                } else {
                    if($page > 0 ) {
                        $link_page .= '<span class="current">' . $page . '</span>';
                    }
                }
            }

            //替换分页内容
            $page_str = str_replace(['%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'],
                                    [self::$config['header'], self::$nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, self::$totalRows, self::$totalPages],
                                    self::$config['theme']);
            return "<div>{$page_str}</div>";
        }
    }
}